package leetcode.code0113;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	List<List<Integer>> ans;

	public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
		ans = new ArrayList<>();
		if (root == null)
			return ans;// null 是错的
		List<Integer> record = new ArrayList<>();
		this.f(root, targetSum, record, 0);
		return ans;
	}

	private void f(TreeNode root, int targetSum, List<Integer> record, int sum) {
		if (root.left == null && root.right == null) {
			if (targetSum == root.val + sum) {
				List<Integer> list = new ArrayList<>(record);
				list.add(root.val);
				ans.add(list);
			}
			return;
		}
		List<Integer> next = new ArrayList<>(record);
		next.add(root.val);
		sum += root.val;
		if (root.left != null) {
			this.f(root.left, targetSum, next, sum);
		}
		if (root.right != null) {
			this.f(root.right, targetSum, next, sum);
		}
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
